package com.meme.editor.model.sprite2d;

import com.meme.editor.EntityObject;

public class Tiled2dBackground extends Tiled2dGrid
{
	

	public Tiled2dBackground ()
	{
		this (Tiled2dLayer.DefaultCellWidth, Tiled2dLayer.DefaultCellHeight, 50, 50);
	}
	
	public Tiled2dBackground (int cellWidth, int cellHeight, int cellRows, int cellCols)
	{
		super (cellWidth, cellHeight, cellRows, cellCols);
		
		Tiled2dLayer defaultLayer = createNewTiled2dLayer ();
		defaultLayer.setCellSize(getCellWidth(), getCellHeight());
		defaultLayer.setGridSize(getCellRows(), getCellCols());
	}

	@Override
	public String getDefaultName ()
	{
		return "Background";
	}
	
	@Override
	public boolean isValidChild (EntityObject entity)
	{
		return entity instanceof Tiled2dLayer;
	}
	
	public Tiled2dLayer createNewTiled2dLayer ()
	{
		addNewChild ();
		
		return (Tiled2dLayer) getChildByIndex (getChildrenCount () - 1);
	}
	
	@Override
	public EntityObject insertNewChild (int insertPos)
	{
		Tiled2dLayer layer = new Tiled2dLayer (getCellWidth(), getCellHeight(), getCellRows(), getCellCols());
		
		layer.getPublicProperty (Tiled2dGrid.GroupName_Tiled2dGridGroup, Tiled2dGrid.PropertyName_CellRows).setEditable(false);
		layer.getPublicProperty (Tiled2dGrid.GroupName_Tiled2dGridGroup, Tiled2dGrid.PropertyName_CellCols).setEditable(false);
		layer.getPublicProperty (Tiled2dGrid.GroupName_Tiled2dGridGroup, Tiled2dGrid.PropertyName_CellWidth).setEditable(false);
		layer.getPublicProperty (Tiled2dGrid.GroupName_Tiled2dGridGroup, Tiled2dGrid.PropertyName_CellHeight).setEditable(false);
		
		return insertChild (layer, insertPos);
	}
	
	@Override int getMaxCellRows () {return Tiled2dLayer.MaxCellRows;}
	@Override int getMaxCellCols () {return Tiled2dLayer.MaxCellCols;}
	
	@Override
	protected void handleGridSizeChanged (int oldRows, int oldCols)
	{
		for (EntityObject child : getChildren ())
		{
			Tiled2dLayer layer = (Tiled2dLayer) child;
			layer.setGridSize(getCellRows(), getCellCols());
		}
	}
	
	@Override
	public boolean setCellSize (int cellWidth, int cellHeight)
	{
		if (super.setCellSize(cellWidth, cellHeight))
		{
			for (EntityObject child : getChildren ())
			{
				Tiled2dLayer layer = (Tiled2dLayer) child;
				layer.setCellSize(getCellWidth(), getCellHeight());
			}
			
			return true;
		}
		
		return false;
	}
	
	public int getLayersCount ()
	{
		return getChildrenCount ();
	}
	
	public Tiled2dLayer getLayerByIndex (int index)
	{
		return (Tiled2dLayer) getChildByIndex (index);
	}
	
	public Tiled2dLayer getLayerByName (String name)
	{
		return (Tiled2dLayer) getChildByName (name);
	}
}
